package cn.xuchunh.campusbbs.ui.fragments.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;

import cn.xuchunh.campusbbs.ui.activities.LoginActivity;
import cn.xuchunh.campusbbs.ui.activities.MainActivity;
import cn.xuchunh.campusbbs.storage.sp.SharedPreferenceManager;
import cn.xuchunh.campusbbs.utils.Constants;

/**
 * Created by XuChunH on 2015/10/12.
 */
public class UIModeSettingDialog extends AppCompatDialogFragment {

    public UIModeSettingDialog() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final SharedPreferenceManager preferenceManager = SharedPreferenceManager.getInstance();
        String[] items = {"侧滑", "底部导航"};
        return builder.setTitle("主页面样式")
                .setSingleChoiceItems(items, preferenceManager.getUiMode(), new DialogInterface
                        .OnClickListener() {


                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int lastUiMode = preferenceManager.getUiMode();
                        if (lastUiMode != which) {
                            if (lastUiMode == Constants.UI_MODE_BOTTOM
                                    && MainActivity.instance != null) {
                                MainActivity.instance.finish();
                            }
                            preferenceManager.setUiMode(which);
                            LoginActivity.launch(getActivity());
                            getActivity().finish();
                        }
                        dialog.dismiss();
                    }
                }).create();
    }
}
